﻿/********************************************************************************************
 * 文件名称:	List_TranImport.cs
 * 设计人员:	严向晖(yanxianghui@gmail.com)
 * 设计时间:	2009-04-10
 * 功能描述:	添加豆列
 * 注意事项:	
 * 版权所有:	Copyright (c) 2009, Fujian SIRC
 * 修改记录: 	修改时间		人员		    修改备注
 *				    ----------		------		-------------------------------------------------
 *
 ********************************************************************************************/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using Shanfree.Framework.Utility;
using Shanfree.SharpMovie.BLL;
using Shanfree.SharpMovie.Model;

public partial class List_TranImport : BasePage
{
    /// <summary>
    /// 列表的主键
    /// </summary>
    public string ListID
    {
        get
        {
            return GetViewState("ListID");
        }
        set
        {
            ViewState["ListID"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                BindMenuList();
            }
        }
        catch (Exception ex)
        {
            LogEntry.Log.Write(ex.ToString(), EventLogEntryType.Error, LogSourceType.一般错误);
            ShowMsg(ex.Message) ;
        }
    }
    private void BindMenuList()
    {
        TreeNodeInfo<CatalogInfo> list = new Catalog().GetTree(CatalogInfo.DEFAULT_PARENT_ID);
        dtMenu.DataSource = list;
        dtMenu.DataBind();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        try
        {
            string fileName = fuPath.FileName;
            string fileExt = Path.GetExtension(fileName);
            if (fileExt != ".txt")
            {
                ShowMsg("只接受文本文件上传。");
                return;
            }
            string listName = Path.GetFileNameWithoutExtension(fileName);
            ListInfo lInfo = new List().GetByName(listName);
            if (null == lInfo || string.IsNullOrEmpty(lInfo.ID))
            {
                tbListName.Text = listName;
                string listRemark = tbListRemark.Text;
                string total = tbTotal.Text;
                string menuID = dtMenu.NodeID;
                this.ListID = new List().Add(listName, listRemark, total, menuID, this.CurrentUser.ID);
                lInfo = new List().GetByID(this.ListID);
                // 加入收藏
                string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
                fuPath.SaveAs(filePath);

                if (File.Exists(filePath) == false)
                {
                    ShowMsg("文件在：" + filePath + "不存在。");
                    return;
                }
                new List().TranImport(this.ListID, filePath, this.CurrentUser.ID);
            }
            else
            {
                // List已存在
                this.ListID = lInfo.ID;
            }
            tbListName.Text = lInfo.Name;
            tbListRemark.Text = lInfo.Remark;
            tbTotal.Text = lInfo.Total.ToString();
            dtMenu.NodeID = lInfo.CatalogID;
            tbListCreateTime.Text = lInfo.CreateTime.ToShortDateString();
            //this.ListID = "27";
            // 重新加载第一页
            pGrid.CurPageNumber = 1;
            BindList(this.ListID);
            ShowMsg("收藏分类导入成功。");
        }
        catch (Exception exc)
        {
            ShowMsg(exc.Message);
            LogEntry.Log.Write(exc.ToString(), EventLogEntryType.Error, LogSourceType.一般错误);
        }
    }

    #region 列表控件事件
    /// <summary>
    /// 翻页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void pGrid_OnClick(object sender, EventArgs e)
    {
        try
        {
            BindList(this.ListID);
        }
        catch (Exception exc)
        {
            ShowMsg(exc.Message);
            LogEntry.Log.Write(exc.ToString(), EventLogEntryType.Error, LogSourceType.一般错误);
        }
    }

    private void BindList(string listID)
    {
        IList<ListItemInfo> list = new List().GetItemList(listID, gvList.PageSize, pGrid.CurrentPageIndex);
        gvList.DataSource = list;
        gvList.DataBind();
        CustomList<ListItemInfo> cList = list as CustomList<ListItemInfo>;
        if (null != cList)
        {
            pGrid.PageSize = gvList.PageSize;
            pGrid.TotalAmout = cList.TotalAmout;
        }
        lbAmount.Text = cList.TotalAmout.ToString();
    }
    #endregion
}
